package com.mjfeed.controller.shop.member;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mjfeed.controller.BaseController;
import com.mjfeed.entity.Member;
import com.mjfeed.entity.Product;
import com.mjfeed.service.MemberService;
import com.mjfeed.service.ProductService;

/**
 * @description 会员收藏(即会员关注)
 * @author zhaominglei
 * @date 2014年12月26日
 */
@Controller("shopFavoriteController")
@RequestMapping("/member/favorite")
public class FavoriteController extends BaseController {
	@Resource(name="memberService")
	private MemberService memberService;
	@Resource(name="productService")
	private ProductService productService;
	
	@RequestMapping(value = "/list" ,method=RequestMethod.GET)
	public String list(HttpServletRequest request, HttpServletResponse response, Model model){
		Member member = memberService.getCurrent();
		if (member == null || member.getId() == null || member.getId() < 1) {
			return ERROR_VIEW;
		}
		request.setAttribute("favoriteProducts", member.getFavoriteProducts());
		return "shop/member/favorite/list";
	}
	
	@RequestMapping(value = "/add" ,method=RequestMethod.POST)
	public @ResponseBody Map<String, Object> add(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> data = new HashMap<String, Object>();
		Member member = memberService.getCurrent();
		if (member == null || member.getId() == null || member.getId() < 1) {
			data.put("success", "false");
			data.put("message", "not login");
			return data;
		}
		Long productId = Long.valueOf(request.getParameter("productId"));
		if (productId == null || productId < 1) {
			data.put("success", "false");
			data.put("message", "productId is empty");
			return data;
		}
		Product product = productService.getById(Product.class, productId);
		if (product == null) {
			data.put("success", "false");
			data.put("message", "product not exists");
			return data;
		}
		if (member.getFavoriteProducts().contains(product)) {
			data.put("success", "false");
			data.put("message", "product has exists");
			return data;
		}
		if (member.getFavoriteProducts().size() == Member.MAX_FAVORITE_COUNT) {
			data.put("success", "false");
			data.put("message", "beyond max favorite count");
			return data;
		}
		member.getFavoriteProducts().add(product);
		memberService.update(member);
		data.put("success", "true");
		data.put("message", "success");
		return data;
	}
	
	@RequestMapping(value = "/delete" ,method=RequestMethod.POST)
	public @ResponseBody Map<String, Object> delete(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> data = new HashMap<String, Object>();
		Member member = memberService.getCurrent();
		if (member == null || member.getId() == null || member.getId() < 1) {
			data.put("success", "false");
			data.put("message", "not login");
			return data;
		}
		Long productId = Long.valueOf(request.getParameter("productId"));
		if (productId == null || productId < 1) {
			data.put("success", "false");
			data.put("message", "productId is empty");
			return data;
		}
		Product product = productService.getById(Product.class, productId);
		if (product == null) {
			data.put("success", "false");
			data.put("message", "product not exists");
			return data;
		}
		if (!member.getFavoriteProducts().contains(product)) {
			data.put("success", "false");
			data.put("message", "product not favorite");
			return data;
		}
		member.getFavoriteProducts().remove(product);
		memberService.update(member);
		data.put("success", "true");
		data.put("message", "success");
		return data;
	}
}
